package com.shure.encrypt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shure.encrypt.entity.TestStudent;
import com.shure.encrypt.service.TestStudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (TestStudent)表控制层
 *
 * @author makejava
 * @since 2022-04-27 09:38:13
 */
@RestController
@RequestMapping("test")
public class TestStudentController {

    @Autowired
    TestStudentService testStudentService;

    @GetMapping("findAll")
    public List<TestStudent> findAll() {
        return testStudentService.list();
    }


    @GetMapping("find")
    public List<TestStudent> find(String name) {
        QueryWrapper<TestStudent> wrapper = new QueryWrapper<TestStudent>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        wrapper.likeLeft("email", "qq.com");
        wrapper.likeLeft("id_card", "2434");
        return testStudentService.list(wrapper);
    }

    @GetMapping("find1")
    public List<TestStudent> find1(String name) {
        LambdaQueryWrapper<TestStudent> wrapper = new LambdaQueryWrapper<TestStudent>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(TestStudent::getName, name);
        }
        wrapper.likeLeft(TestStudent::getEmail, "qq.com");
        wrapper.likeLeft(TestStudent::getIdCard, "2434");
        return testStudentService.list(wrapper);
    }

    @GetMapping("find2")
    public List<TestStudent> find2() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张小鸡");
        map.put("id_Card", "622323198909142434");
        return testStudentService.listByMap(map);
    }

    @PostMapping("save")
    public TestStudent save(@RequestBody TestStudent student) {
        if (student != null) {
            if (student.getSid() != null) {
                student.setUpdateTime(new Date());
            }
            testStudentService.saveOrUpdate(student);
        }
        return student;
    }

    @PostMapping("update")
    public Boolean update(@RequestBody TestStudent student) {
        if (student != null && null != student.getSid()) {
            student.setUpdateTime(new Date());
            testStudentService.update(student);
            return true;
        }
        return false;
    }
}

